#pragma once
#include "model.h"

#include "global_event_define.h"
#include "eventsocket.h"
#include <vector>
typedef std::vector<GO_INSTANCE_STATE_T*> GO_INSTANCE_VECTOR;
class DLL_EXP CWeiQiClient: public CEventSocket, public CEventDispatcher
{
public:

	char m_playerName[MAX_PLAYER_NAME];
	WEIQI_SIDE_T m_chessSide;
	int m_gameSize;
	int m_numGoInstances;
	int m_nInstanceId;
	int m_playerId;
	std::vector<GO_PLAYER_STATE_T*> m_vectorPlayerStates;
	GO_INSTANCE_VECTOR m_vectorInstances;

	STATE_MACHINE_T* m_pStateTable;


	GO_PLAYER_STATE_T* GetPlayerState(unsigned int player_id);

	GO_INSTANCE_STATE_T* GetGoInstance(unsigned int instance_id);

	GO_INSTANCE_STATE_T* UpdateInstance(GO_INSTANCE_STATE_T* pInstance);
	void ClearInstances();
	void RemoveInstance(unsigned int instance_id);
	BOOL RefreshInstances(GO_INSTANCE_STATE_T* pInstance, int num);
	unsigned int GetNumInstances();
	GO_PLAYER_STATE_T* UpdatePlayerState(GO_PLAYER_STATE_T* pPlayer);
	CWeiQiClient();
	~CWeiQiClient();

	void DefineStateMachine();
	virtual STATE_MACHINE_T* GetStateMachineTable();

	void InitListener();
	void init();

	static BOOL HandleSocketListGameRsp(CSocketStateMachine* me,winSocketEx* client,CMsg *pMsg);

	static BOOL HandleSocketInfoGameRsp(CSocketStateMachine* me,winSocketEx* client,CMsg *pMsg);

	static BOOL HandleSocketCreateGameRsp(CSocketStateMachine* me,winSocketEx* client,CMsg *pMsg);
	static BOOL HandleSocketJoinGame(CSocketStateMachine* me,winSocketEx* client,CMsg *pMsg);
	static BOOL HandleSocketStartGame(CSocketStateMachine* me,winSocketEx* client,CMsg *pMsg);
	static BOOL HandleSocketJoinGameRsp(CSocketStateMachine* me,winSocketEx* client,CMsg *pMsg);

	static BOOL HandleSocketExitGameRsp(CSocketStateMachine* me,winSocketEx* client,CMsg *pMsg);

	static BOOL HandleSocketPlayerExit(CSocketStateMachine* me,winSocketEx* client,CMsg *pMsg);

	static BOOL HandleSocketPlayerDisconnect(CSocketStateMachine* me,winSocketEx* client,CMsg *pMsg);
	static BOOL HandleSocketGameUpdated(CSocketStateMachine* me,winSocketEx* client,CMsg *pMsg);
		static BOOL HandleSocketPlayerPass(CSocketStateMachine* me,winSocketEx* client,CMsg *pMsg);
	static BOOL HandleSocketDianMuResult(CSocketStateMachine* me,winSocketEx* client,CMsg *pMsg);
	static BOOL HandleSocketMoveChess(CSocketStateMachine* me,winSocketEx* client,CMsg *pMsg);
	static BOOL HandleSocketNextTurn(CSocketStateMachine* me,winSocketEx* client,CMsg *pMsg);
	static BOOL HandleSocketDragonDead(CSocketStateMachine* me,winSocketEx* client,CMsg *pMsg);


	void CreateGame(char* name,WEIQI_SIDE_T side,int game_size);

	void ListGame(char* name);
	void InfoGame(unsigned int instance_id);
	void HandleUIMoveChess(EVENT_MOVE_CHESS_T* evt);
	void HandleUIPlayerPass(EVENT_PLAYER_PASS_T* evt);
	void HandleUICreateGame(char* name,WEIQI_SIDE_T side,int game_size);
	void HandleUIReqDianMu(EVENT_REQ_DIANMU_T* evt);
	void JoinGame(char* name,WEIQI_SIDE_T side,unsigned int instance_id);
	void JoinGameRsp(EVENT_JOIN_GAME_RSP_T* evt);
	void ExitGame(EVENT_EXIT_GAME_T* evt);
	static void handleUIEvent(void *pUserData, ModelEvent *pEvent);
	void HelloServer(char* name);
};